//
//  NSArray+TYExtendsion.m
//  TYToolKit
//
//  Created by dev_tianyu on 16/8/8.
//  Copyright © 2016年 dev_tianyu. All rights reserved.
//

#import "NSArray+TYExtendsion.h"

@implementation NSArray (TYExtendsion)

/**
 *  数组转字符串
 */
- (NSString *) arrayToString {
    if (self == nil || self.count == 0) {
        return @"";
    }
    
    NSMutableString *str = [NSMutableString string];
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [str appendFormat:@"%@,",obj];
    }];
    
    NSString *result = [str substringWithRange:NSMakeRange(0, str.length-1)];
    return result;
}

/**
 *  数组比较
 */
- (BOOL) compareObjectWithArray:(NSArray *)array {
    NSSet *set1 = [NSSet setWithArray:self];
    NSSet *set2 = [NSSet setWithArray:array];
    
    return [set1 isEqualToSet:set2];
}

/**
 *  获取当前数组特有元素
 */
-(NSArray *)arrayForIntersectionWithOtherArray:(NSArray *)otherArray{
    
    if(self.count==0) return nil;
    if(otherArray==nil) return nil;
    
    NSMutableArray *intersectionArray=[NSMutableArray array];
    //遍历
    for (id obj in self) {
        if(![otherArray containsObject:obj]) continue;
        //添加
        [intersectionArray addObject:obj];
    }
    return intersectionArray;
}



/**
 *  获取数组共有元素
 */
-(NSArray *)arrayForMinusWithOtherArray:(NSArray *)otherArray{
    
    if(self==nil) return nil;
    if(otherArray==nil) return self;
    
    NSMutableArray *minusArray=[NSMutableArray arrayWithArray:self];
    //遍历
    for (id obj in otherArray) {
        if(![self containsObject:obj]) continue;
        //添加
        [minusArray removeObject:obj];
    }
    return minusArray;
}

@end
